package com.powerwyx.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.powerwyx.shortlink.project.common.convention.result.Result;
import com.powerwyx.shortlink.project.common.convention.result.Results;
import com.powerwyx.shortlink.project.handler.CustomBlockHandler;
import com.powerwyx.shortlink.project.pojo.dto.ShortLinkBatchCreateDTO;
import com.powerwyx.shortlink.project.pojo.dto.ShortLinkCreateDTO;
import com.powerwyx.shortlink.project.pojo.dto.ShortLinkPageDTO;
import com.powerwyx.shortlink.project.pojo.dto.ShortLinkUpdateDTO;
import com.powerwyx.shortlink.project.pojo.vo.ShortLinkBatchCreateVO;
import com.powerwyx.shortlink.project.pojo.vo.ShortLinkCountVO;
import com.powerwyx.shortlink.project.pojo.vo.ShortLinkCreateVO;
import com.powerwyx.shortlink.project.pojo.vo.ShortLinkPageVO;
import com.powerwyx.shortlink.project.service.LinkService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 王艺锡
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/shortlink/v1")
public class LinkController {
    @Autowired
    private LinkService linkService;

    /**
     * 创建短链接
     * @param shortLinkCreateDTO
     * @return
     */
    @PostMapping("/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateVO> createShortLink(@RequestBody ShortLinkCreateDTO shortLinkCreateDTO){
        ShortLinkCreateVO shortLinkCreateVO = linkService.createShortLink(shortLinkCreateDTO);
        return Results.success(shortLinkCreateVO);
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateVO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateDTO requestParam) {
        return Results.success(linkService.batchCreateShortLink(requestParam));
    }


    /**
     * 根据分组进行分页查询短链接
     * @param shortLinkPageDTO
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<ShortLinkPageVO>> shortLinkPage(ShortLinkPageDTO shortLinkPageDTO){
        return Results.success(linkService.shortLinkPage(shortLinkPageDTO));
    }



    /**
     * 查询分组内短链接数量
     * @return
     */
    @GetMapping("/count")
    public Result<List<ShortLinkCountVO>> getGroupShortLinkCount(@RequestParam("gids") List<String> gids){
        List<ShortLinkCountVO> shortLinkCountVOList = linkService.getGroupShortLinkCount(gids);
        return Results.success(shortLinkCountVOList);
    }


    /**
     * 修改短链接信息
     * @param shortLinkUpdateDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateDTO shortLinkUpdateDTO){
        linkService.updateShortLink(shortLinkUpdateDTO);
        return Results.success();
    }


    /**
     * 短链接跳转
     * @param shortUri
     * @param request
     * @param response
     */
    @GetMapping("/{shortUri}")
    public Result<Void>  restoreUrl(@PathVariable("shortUri") String shortUri, HttpServletRequest request, HttpServletResponse response){
        linkService.restoreUrl(shortUri,request,response);
        return Results.success();
    }


    @PostMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response){
        String fullShortUrl = "nurl.ink:9001/4Hyq7H";
        String gid = "f1lscs";
        linkService.shortLinkStatistics(fullShortUrl,request,response);
    }

}
